<script lang="ts">
export default {
  name: "Login",
  methods:{
    login(){
      const username = this.$refs.username.value.trim();
      const password = this.$refs.password.value.trim();
      if(username && password){
        let result = false;
        this.$store.state.userInfo.forEach(item=>{
          if(item.username === username && item.password === password){
            result = true;
          }
        });
        if(result){
          alert('登录成功');
          localStorage.setItem('loginInfo',JSON.stringify({username}))
          this.$router.push(this.$route.query.returnUrl || "/")
        }else {
          alert('账号或密码错误')
        }
      }else {
        alert('格式错误')
      }
    }
  }
}
</script>

<template>
<div>
  <h3>请登录</h3>
  <input ref="username" type="text" placeholder="请输入用户名">
  <input ref="password" type="password" placeholder="请输入密码">
  <button @click="login">登录</button>
</div>
</template>

<style scoped>

</style>
